package com.fr.lintcode;

import java.util.Stack;

import org.junit.Test;

/**
*作者：furong
*日期：2017年2月8日
*时间：上午10:26:06
*/
public class Q40 {
    public class Queue {
        private Stack<Integer> stack1;
        private Stack<Integer> stack2;

        public Queue() {
            stack1 = new Stack<Integer>();
            stack2 = new Stack<Integer>();
        }

        public void push(int element) {
            while (!stack2.empty()) {
                stack1.push(stack2.pop());
            }
            stack1.push(element);
        }

        public int pop() {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            if (stack2.empty()) {
                return 0;
            }
            return stack2.pop();
        }

        public int top() {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
            if (stack2.empty()) {
                return 0;
            }
            return stack2.peek();
        }
    }

    @Test
    public void testA() {
        Queue q = new Queue();
        q.push(1);
        System.out.println(q.pop());
        q.push(2);
        q.push(3);
        System.out.println(q.top());
        System.out.println(q.pop());
    }
}
